/*
 * Copyright (c) 2009 Project E7
 *
 * This file is part of ZebBowling
 *
 * ZebBowling is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ZebBowling is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ZebBowling. If not, see <http://www.gnu.org/licenses/>.
 **/

package org.sunspotworld;

import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.util.*;

import com.typezero.control.spot.JoyStick;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * This should be deployed to any SPOT that will be used as a {@linkplain JoyStick}
 * for Zebulon Bowling.
 */
public class ZebBowlingJoyStick extends MIDlet {

    private EDemoBoard bd = EDemoBoard.getInstance();
    private JoyStick js;

    /**
     * Starts this {@code JoyStick} when SPOT is turned on
     *
     * @throws javax.microedition.midlet.MIDletStateChangeException
     */
    protected void startApp() throws MIDletStateChangeException {
        new BootloaderListener().start();
        js = new JoyStick(bd);
        new Thread(js).start();
        }        

    protected void pauseApp() {
        // This is not currently called by the Squawk VM
        }

    /**
     * Called if the MIDlet is terminated by the system.
     * I.e. if startApp throws any exception other than MIDletStateChangeException,
     * if the isolate running the MIDlet is killed with Isolate.exit(), or
     * if VM.stopVM() is called.
     * 
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true when this method is called, the MIDlet must
     *    cleanup and release all resources. If false the MIDlet may throw
     *    MIDletStateChangeException  to indicate it does not want to be destroyed
     *    at this time.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        System.out.println("Connection Closing");
        js.close();
        }

    }
